﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using System.IO;
using System.Text;
namespace IDotNet.MapLibs.Web
{
    public class BasePage : UserControl
    {

        /// <summary>
        /// 获取请求参数（先尝试从http请求头获取，再尝试从请求流中获取）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        protected virtual T GetParam<T>()
        {
            var defData = default(T);
            var data = GetParamByHeader<T>();
            if (data != null)
            {
                return data;
            }

            data = GetParamByStream<T>();
            if (data != null)
            {
                return data;
            }

            return defData;
        }

        /// <summary>
        /// 从http请求流中获取数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        protected virtual T GetParamByStream<T>()
        {
            if (Request.InputStream.Length != 0)
            {
                Request.InputStream.Position = 0;
                string data;
                using (var ms = new MemoryStream())
                {
                    Request.InputStream.CopyTo(ms);
                    data = Encoding.UTF8.GetString(ms.ToArray());
                }
                if (!string.IsNullOrWhiteSpace(data))
                {
                    return JsonConvert.DeserializeObject<T>(data);
                }
            }
            return default(T);
        }

        /// <summary>
        /// 从Http头部中获取数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="paramName"></param>
        /// <returns></returns>
        protected virtual T GetParamByHeader<T>(string paramName = "reqParamData")
        {
            var data = Request[paramName];
            if (!string.IsNullOrWhiteSpace(data))
            {
                return JsonConvert.DeserializeObject<T>(data);
            }
            return default(T);
        }

        /// <summary>
        /// 将对象序列化成json（默认值不转化）
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        protected virtual string ToJsonString(object obj)
        {
            return JsonConvert.SerializeObject(obj, new JsonSerializerSettings()
            {
                DefaultValueHandling = DefaultValueHandling.Ignore
            });
        }

        protected virtual string MapAk
        {
            get
            {
                return ConfigurationManager.AppSettings["BMap.Ak.Web"];
            }
        }

        protected virtual void ShowMsg(string msg)
        {
            Response.Write("<script/>");
            Response.Write("alert('");
            Response.Write(msg);
            Response.Write("');");
            Response.Write("</script>");
            Response.End();
        }

    }
}